/*
Copyright 2005-2014 CP Fricano - All Rights Reserved

This software and source code is licensed under the Apache
License, Version 2.0. You may not use this software or source
code except in compliance with the License.  You may obtain a
copy of the License at: http://www.apache.org/licenses/LICENSE-2.0.
Use and distributed under this License is on an "as is" basis,
without expressed or implied warranties or conditions of any kind.
See the License for the specific language governing permissions and
limitations.
 */

package com.fricano.java42.weather.api.provider.impl;

import java.util.Map;

import com.fricano.java42.base.components.datapointprovider.impl.ProviderConnector_JSON;
import com.fricano.java42.base.components.datapointprovider.impl.ProviderDataPointMap;
import com.fricano.java42.base.components.utility.impl.Utility;
import com.fricano.java42.weather.api.provider.adapter.Key;
import com.fricano.java42.weather.api.provider.adapter.WeatherProviderInfo;

public class WeatherUndergroundProviderInfo extends WeatherProviderInfo {

    // http://www.wunderground.com/weather/api/
    // http://www.wunderground.com/weather/api/d/docs

    public WeatherUndergroundProviderInfo(final Object apiKey) {
        super(apiKey);
        super._query = "27613";
        super._dataPointMap.put(Key.TIME, new String[] { "current_observation", "observation_epoch" });
        super._dataPointMap.put(Key.TEMPERATURE, new String[] { "current_observation", "temp_f" });
        super._dataPointMap.put(Key.LOCATION_FULL, new String[] { "current_observation", "display_location", "full" });
        // super._dataPointMap.put(Key.TIME_RFC822, new String[] { "current_observation", "observation_time_rfc822" });
        // super._dataPointMap.put(Key.DATE, new String[] { "current_observation", "observation_epoch" });
        // super._dataPointMap.put(Key.LOCATION_ZIP, new String[] { "current_observation", "display_location", "zip" });
        // super._dataPointMap.put(Key.LOCATION_COUNTRY, new String[] { "current_observation", "display_location", "country" });
    }

    @Override
    public ProviderDataPointMap<Object, Object> getDataPointMap() {
        return super._dataPointMap;
    }

    @Override
    protected String getDebugBody() {
        final String debugBody = "{\"response\":{\"features\":{\"conditions\":1},\"termsofService\":\"http://www.wunderground.com/weather/api/d/terms.html\",\"version\":\"0.1\"},\"current_observation\":{\"wind_gust_mph\":0,\"precip_1hr_metric\":\" 0\",\"precip_today_metric\":\"0\",\"pressure_trend\":\"0\",\"forecast_url\":\"http://www.wunderground.com/US/NC/Raleigh.html\",\"history_url\":\"http://www.wunderground.com/weatherstation/WXDailyHistory.asp?ID=KNCRALEI47\",\"underGround_Estimated\":{},\"weather\":\"Mostly Cloudy\",\"windchill_string\":\"42 F (2 C)\",\"station_id\":\"KNCRALEI47\",\"UV\":\"0\",\"observation_epoch\":\"0\",\"wind_gust_kph\":0,\"precip_1hr_in\":\"0.00\",\"observation_time\":\"Last Updated on February 2, 00:42 AM EST\",\"feelslike_string\":\"42 F (2 C)\",\"temp_f\":42.42,\"local_tz_long\":\"America/New_York\",\"relative_humidity\":\"78%\",\"temp_c\":2.3,\"image\":{\"title\":\"Weather Underground\",\"link\":\"http://www.wunderground.com\",\"url\":\"http://icons-ak.wxug.com/graphics/wu2/logo_130x80.png\"},\"solarradiation\":\"--\",\"visibility_mi\":\"10.0\",\"observation_location\":{\"full\":\"NC State Area, Raleigh, North Carolina\",\"elevation\":\"350 ft\",\"state\":\"North Carolina\",\"longitude\":\"-78.674400\",\"latitude\":\"35.798538\",\"country_iso3166\":\"US\",\"country\":\"US\",\"city\":\"NC State Area, Raleigh\"},\"wind_mph\":0,\"heat_index_c\":\"NA\",\"precip_today_string\":\"0.00 in (0 mm)\",\"observation_time_rfc822\":\"Fri, 07 Feb 2014 03:51:00 -0500\",\"feelslike_f\":\"36\",\"heat_index_f\":\"NA\",\"feelslike_c\":\"2\",\"heat_index_string\":\"NA\",\"ob_url\":\"http://www.wunderground.com/cgi-bin/findweather/getForecast?query=35.798538,-78.674400\",\"dewpoint_string\":\"30 F (-1 C)\",\"local_tz_offset\":\"-0500\",\"wind_kph\":0,\"windchill_f\":\"36\",\"windchill_c\":\"2\",\"wind_degrees\":28,\"pressure_in\":\"30.28\",\"dewpoint_c\":-1,\"pressure_mb\":\"1025\",\"icon\":\"mostlycloudy\",\"local_time_rfc822\":\"Fri, 07 Feb 2014 03:51:17 -0500\",\"precip_1hr_string\":\"0.00 in ( 0 mm)\",\"icon_url\":\"http://icons-ak.wxug.com/i/c/k/nt_mostlycloudy.gif\",\"wind_dir\":\"NNE\",\"dewpoint_f\":30,\"display_location\":{\"zip\":\"27601\",\"magic\":\"1\",\"full\":\"Raleigh, NC\",\"elevation\":\"90.00000000\",\"state\":\"NC\",\"wmo\":\"99999\",\"longitude\":\"-78.63275909\",\"latitude\":\"35.77399445\",\"state_name\":\"North Carolina\",\"country_iso3166\":\"US\",\"country\":\"US\",\"city\":\"Raleigh\"},\"visibility_km\":\"16.1\",\"temperature_string\":\"36.2 F (2.3 C)\",\"local_tz_short\":\"EST\",\"local_epoch\":\"1391763077\",\"wind_string\":\"Calm\",\"precip_today_in\":\"0.00\"}}";
        return debugBody;
    }

    @Override
    protected String getHostName() {
        return "api.wunderground.com";
    }

    @Override
    protected int getPort() {
        return 80;
    }

    @Override
    protected String getProtocol() {
        return "http";
    }

    @Override
    protected String getProviderClassName() {
        return ProviderConnector_JSON.class.getName();
    }

    @Override
    public String getProviderName() {
        return "WeatherUnderground";

    }

    @Override
    protected String getQuery() {
        return "/api/" + super.getApiKey() + "/conditions/q/" + super._query + ".json";
    }

    @Override
    public Map<String, String> getRequestHeaders() {
        return null;
    }

    @Override
    public void setQuery(final String query) {
        super._query = query;
    }

    @Override
    protected Object translateDataPoint(final Object key, final Object value) {
        Object result = value;
        if (key.equals(Key.TIME) && value.getClass() == String.class) {
            result = Utility.translateUnixTime(Integer.parseInt((String) value));
        }
        if (key.equals(Key.TIME_RFC822) && value.getClass() == String.class) {
            result = Utility.translateRFC822Date((String) value);
        }
        if (key.equals(Key.DATE) && value.getClass() == String.class) {
            result = Utility.translateUnixTime(Integer.parseInt((String) value));
        }
        return result;
    }

    @Override
    protected void translateFinal() {
    }

    @Override
    protected void translateQuery() {
    }
}
